home *** CD-ROM | disk | FTP | other *** search
- Path: news.duke.edu!stking
- From: stking@acpub.duke.edu (Steve King)
- Newsgroups: comp.lang.c
- Subject: Problem w/simple linked list
- Date: 18 Apr 1996 13:02:34 GMT
- Organization: Duke University, Durham, NC, USA
- Message-ID: <4l5eha$5qa@news.duke.edu>
- NNTP-Posting-Host: carr3.acpub.duke.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- Folks, I'm forgetting something here. Here's my problem. I'm
- trying to compare a name I input with the names already
- stored in nodes in my linked list. If there's a match,
- I want to delete that node. However, I can't ever seem to
- get a match using "strcmp()."
-
- Here's the relevant code:
-
- /* Assign name to whatever node I'm working on in */
- /* the linked list.... */
-
- printf("Please enter the owner's name: ");
- fgets(current->patient.name, MAX_NAME,stdin);
- NoNewLine(current->patient.name);
- .......
-
- /* Prompt for name to delete, pass it to the */
- /* function "FindInList()" which should return*/
- /* a pointer to the previous node if there's */
- /* a match, otherwise NULL. But it always */
- /* returns NULL, even though I know the name */
- /* I enter is in the list! Why? */
- case 3 :
- printf("Enter name to delete: ");
- fgets(name,MAX_NAME,stdin);
- NoNewLine(name);
- current = FindInList(head, name);
- if (current == NULL)
- {
- printf("Name not found\n");
- break;
- }
- temp = current->next;
- current->next = temp->next;
- free(temp);
-
-
- /* Relevant functions....*/
-
- /* Could be a macro -- shouldn't need explanation */
- void NoNewLine(
- char * string)
- {
- if ((string) [strlen(string) -1] == '\n') \
- (string) [strlen(string) -1] = '\0';
- }
-
- /* Function to walk through list and check for a "match" */
- struct APLIST * FindInList(
- const struct APLIST *node,
- const char * name)
- {
- struct APLIST * walk;
- struct APLIST * previous;
- for (walk = node, previous = NULL; walk; walk = walk->next)
- {
- if (strcmp(walk->patient.name,name) == 0)
- {
- return(previous);
- }
- }
- previous = walk;
- return(NULL);
- }
-
-
- Many thanks -- I'm truly perplexed by the constant return
- of "NULL" when I call this function from my switch/case.
-
-
-
- ======================================================================
- *stking@acpub.duke.edu*| Such as they are, these are my opinions only;
- *919-286-4476* | not my employer's or internet provider's....
- ======================================================================
-
-